package com.xyz.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyz.entity.LoginLog;

import com.xyz.entity.vo.ListLoginLogVO;
import com.xyz.mapper.LoginLogMapper;
import com.xyz.service.ILoginLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2022-04-27
 */
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements ILoginLogService {

    @Override
    public Page<ListLoginLogVO> listLoginLog(String idNumber, Integer page, Integer limit) {
        LambdaQueryChainWrapper<LoginLog> wrapper = this.lambdaQuery();
        if (Objects.nonNull(idNumber)) {
            wrapper.eq(LoginLog::getIdNumber, idNumber);
        }
        Page<LoginLog> page1 = wrapper.page(new Page<>(page, limit));
        List<ListLoginLogVO> collect = page1.getRecords().stream().map(o -> {
            ListLoginLogVO item = new ListLoginLogVO();
            BeanUtils.copyProperties(o, item);
            return item;
        }).collect(Collectors.toList());
        Page<ListLoginLogVO> listLoginLogVOPage = new Page<>();
        BeanUtils.copyProperties(page1, listLoginLogVOPage);
        return listLoginLogVOPage.setRecords(collect);
    }
}
